#!/bin/sh

# /etc/rc.d/rc.exim - start/stop/restart the exim mail transfer agent.
# To make exim start automatically at boot, make this
# file executable: chmod 755 /etc/rc.d/rc.exim
#
# Thales A. Tsailas <ttsailas@enforcingit.com>

PIDFILE=/var/run/exim.pid

# the TIME option causes Exim to run as a daemon, starting a queue runner 
# process at intervals specified by the given time value. (ie 5m, 1h etc).
TIME=15m

exim_start() {
    # Make sure that we have the right ownerships permissions.
    if [ -f /var/log/exim/main.log ]; then
        if [ "exim" != "$(/bin/stat -c%G /var/log/exim/main.log)" ]; then
            chown -R exim:exim /var/{log,spool}/exim
        fi
    fi
    
    # Lets start the Exim daemon
    echo -en "Starting exim... \n"
    /usr/sbin/exim -bd -q$TIME
}

exim_stop() {
    echo -en "Shutting down exim...\n"
    killall exim
    rm -f $PIDFILE
}

exim_status() {
    if [ -f /var/run/exim.pid ]; then
        echo "exim (pid: $(cat $PIDFILE) is running...";
    else 
        echo "exim is not running...";
    fi
}

# See how we were called.
case "$1" in
  start)
    exim_start
    ;;
  stop)
    exim_stop
    ;;
  restart)
    exim_stop
    sleep 2
    exim_start
    ;;
  status)
    status 
    ;;
  *)
    echo $"Usage: $0 {start|stop|restart|status}"
    exit 1
esac
